package org.example.juc.p14;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author lou ke
 * @since 2024/2/13 8:50
 */
@Slf4j
public class JoinThread {


    static int value = 1;

    /**
     * 线程合并join
     *
     * @param args
     */
    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

                // todo 。。。业务代码
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                value = 10;
                log.info("线程Runnable...");
            }
        });

        thread.start(); // 执行子线程

        log.info("子线程是否存活：{}",thread.isAlive());
        // TimeUnit.SECONDS.sleep(1);
        thread.join(); // 加上join后，主线程就会等待子线程执行完毕后再继续执行  可以理解为将异步变成同步
        log.info("子线程是否存活：{}",thread.isAlive());
        log.info("主线程value：{}", value);

    }

}
